package org.yyjkcache.common;

import java.io.IOException;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class HttpDataClient {
	private CloseableHttpClient client = null;
	private List<NameValuePair> nvps = null;
	private String url = null;
	/**
	 * 登录
	 * @param nvps
	 * @param Url
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public void login(List<NameValuePair> nvps, String Url) throws ClientProtocolException, IOException {
		this.nvps = nvps;
		this.url = Url;
		CookieStore store = new BasicCookieStore();
		CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(store).build();
		HttpPost post = new HttpPost(Url);
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
		post.setEntity(formEntity);
		httpclient.execute(post);
		// post.releaseConnection();
		this.client = httpclient;
	}

	public CloseableHttpClient getClient() {
		return client;
	}

	public void loginAgain() throws ClientProtocolException, IOException {
		login(nvps, url);
	}

	public void validate() {

	}
}
